﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Mengsk.ProxyDelivery.Exporter
{
    public class YouzhenXiaoBaoExporter : IDeliveryOutExporter
    {
        public bool Accept(string deliveryCompanyName)
        {
            return deliveryCompanyName.Contains("小包");
        }

        public void Export(ExportInfo exportInfo, Domain.DeliveryOut[] deliveryOuts)
        {
            IExportFile file = new XlsExportFile();
            try
            {
                file.ExportInfo = exportInfo;
                file.Open();

                //写入头
                file.WriteData(new string[] { "序号", "订单号", "邮件号码", "收件人", "收件人手机", "收件详细地址(省 市 县 详细地址)", "收件地邮编", "重量", "数量", "货物描述", "备注", "代收款" }.Select(obj => new ExportData { Name = "", Value = obj }).ToArray());
                ExportData[] datas = new ExportData[6];
                int i = 1;
                OrderService os = ServiceContainer.GetService<OrderService>();
                foreach (var o in deliveryOuts)
                {
                    datas[0] = new ExportData { Name = "", Value = i.ToString() };
                    datas[1] = new ExportData { Name = "", Value = o.OrderId.ToString() };
                    datas[2] = new ExportData { Name = "", Value = o.DeliveryNumber.ToString() };
                    Order or = os.GetById(o.OrderId);
                    if (or == null)
                    {
                        MessageBox.Show("发货编号未找到指定的订单:" + o.Id + "," + o.OrderId);
                        continue;
                    }
                    datas[3] = new ExportData { Name = "", Value = or.ReceiverName };
                    if (string.IsNullOrWhiteSpace(or.ReceiverMobile))
                    {
                        datas[4] = new ExportData { Name = "", Value = or.ReceiverPhone };
                    }
                    else
                    {
                        datas[4] = new ExportData { Name = "", Value = or.ReceiverMobile };
                    }
                    datas[5] = new ExportData { Name = "", Value = or.ReceiverAddress };
                    file.WriteData(datas);
                    i++;
                }
            }
            finally
            {
                if (file != null && file.IsOpened)
                {
                    file.Close();
                }
            }
        }
    }
}
